package com.fayuan.nocv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fayuan.nocv.entity.Office;
import com.fayuan.nocv.entity.XueYuan;
import com.fayuan.nocv.service.OfficeService;
import com.fayuan.nocv.vo.DataView;
import com.fayuan.nocv.vo.OfficeVo;
import com.fayuan.nocv.vo.XueYuanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/office")
public class OfficeController {

    @Autowired
    private OfficeService officeService;

    @GetMapping("/listOffice")
    @ResponseBody
    public DataView listXueYuan(OfficeVo officeVo){

        QueryWrapper<Office>wrapper = new QueryWrapper<>();
        if (officeVo.getName()!=null){
            wrapper.like("name",officeVo.getName());
        }
        IPage<Office>page = new Page<>();
        IPage<Office> page1 = officeService.page(page, wrapper);
        return new DataView(page1.getTotal(),page1.getRecords());
    }

    @PostMapping("/addOrUpdateOffice")
    @ResponseBody
    public DataView addOrUpdateXueYuan(Office office){

        officeService.saveOrUpdate(office);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("部门操作成功");
        return dataView;
    }

    @PostMapping("/deleteById")
    @ResponseBody
    public DataView DeleteXueYuan(Office office){

        officeService.removeById(office.getId());
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("部门删除成功");
        return dataView;
    }
}
